//
//  ViewController.m
//  HYHOME
//
//  Created by Mac on 16/1/20.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "ViewController.h"
#import "MJRefresh.h"
#import "HeadTableViewCell.h"
#import "NetWorkHandler.h"
#import "UIImageView+WebCache.h"
#import "ModelData.h"
#import "NetworkingHandle.h"
#import "Modelcontent.h"
#import "CellofContent.h"
#import "HeadImageView.h"
#import "PushVc1.h"
#import "Image.h"
#import "URLViewController.h"
#import "DKNightVersion.h"
#import "Search.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,NetWorkHandlerDelegate,UIScrollViewDelegate, HeadImageViewDelegate>

@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)NSMutableArray *arrModelData;
@property (nonatomic,retain)NSMutableArray *arrModelContent;


@property (nonatomic, assign) NSInteger page;
@property (nonatomic, retain) NSString *strpage;
@property (nonatomic, retain) NSString *str;

@end

@implementation ViewController



- (void)dealloc{
    [_tableView release];
    [_str release];
    [_strpage release];
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    [super dealloc];



}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.str = [[NSString alloc] init];
    self.arrModelContent = [NSMutableArray array];
    // Do any additional setup after loading the view, typically from a nib.


    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.automaticallyAdjustsScrollViewInsets = NO;

    
    self.title = @"首页";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"fenlei"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
    
    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchAction)];
    
    
    
    
    
    [self createtableView];
    [self handleData];
    [self handleWithStr:nil];
    

}


- (void)searchAction{


  
    Search *search = [[Search alloc]init];
    [self.navigationController pushViewController:search animated:YES];


    [search release];
}


- (void)handleWithStr:(NSString *)str {
    if (!str) {
        str = @"http://api.bohejiaju.com/v1/channels/15/items?limit=20&offset=0";
    }
    
    [NetworkingHandle networkingWithURL:str completeHandel:^(NSURLResponse *response, NSData *data, id result) {
        
        
        
        
        NSDictionary *dic = [result objectForKey:@"data"];
        
        NSArray *arr = [dic objectForKey:@"items"];
        for (NSDictionary *dic2 in arr) {
            
            Modelcontent *model = [[Modelcontent alloc]init];
            model.cover_image_url = [dic2 objectForKey:@"cover_image_url"];
            model.title = [dic2 objectForKey:@"title"];
            model.content_url = [dic2 objectForKey:@"content_url"];
            model.ida = [dic2 objectForKey:@"id"];
            model.likes_count = [dic2 objectForKey:@"likes_count"];
            
            [self.arrModelContent addObject:model];
            
            [model release];
            
        }
        
        NSDictionary *dicPaging = [dic objectForKey:@"paging"];
        self.str = [dicPaging objectForKey:@"next_url"];
        
        
        
        
        [self.tableView reloadData];
    }];
    


}

- (void)handleData{
    



//    NSString *str = @"http://mobileapi.to8to.com/index.php?action=get&addtype=2&appid=1&appname=%E8%A3%85%E4%BF%AE%E7%AE%A1%E5%AE%B6&appostype=2&appversion=3.2.4&channel=appstore&idfa=EEACF35A-5B73-4678-B6EF-E188A26B0232&imagesize=320&module=ad&systemversion=9.1&t8t_device_id=82FF5982-855F-471B-8D56-30D0AA7F7D6F&to8to_token=&uid=0&version=2.5&vesion=2.";
    
//    NSString *str = @"http://portal-web.zhaidou.com/index/getBoardContent.action?boardCodes=01,02,03&pageNo=1";
    NSString *str = @"http://api.nuanjia.cn/adverts/main";
    
    [NetWorkHandler handlerJSONWithURL:str delegate:self];

}

- (void)haneldDidComplete:(id)result{
    self.arrModelData = [NSMutableArray array];
    

    
    for (int i = 0; i<4; i++) {
    
        
        NSDictionary *dicre = result[i];
        ModelData *model = [[ModelData alloc]init];
 
            model.imgurl = [dicre objectForKey:@"picture"];
        
        
            model.linkurl = [dicre objectForKey:@"target"];
        
            NSMutableString *str = model.linkurl.mutableCopy;
            NSRange range = [str rangeOfString:@"njTopics"];
            [str replaceCharactersInRange:range withString:@"http"];
        
                model.linkurl = str;
                [self.arrModelData addObject:model];
        
        
        [model release];
        
    }
    
//
    if (self.arrModelData.count) {
        [self.arrModelData addObject:self.arrModelData.firstObject];
        [self.arrModelData insertObject:self.arrModelData[self.arrModelData.count - 2] atIndex:0];
    }
    [self.tableView reloadData];
    
    
    
}


- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];


}

- (void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];
}
- (void)createtableView{

    
    

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height)style:UITableViewStylePlain ];
    

    
   
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.tableView.mj_header endRefreshing];
            
        });
        
        
        
    }];
    
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    
   //上拉加载
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self handleData];
            [self handleWithStr:self.str];
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
            
        });
        
        
    }];

    
    [self.tableView.mj_header beginRefreshing];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
    [self.tableView registerClass:[HeadTableViewCell class] forCellReuseIdentifier:@"pool"];
    
    
    
    [self.tableView registerClass:[CellofContent class] forCellReuseIdentifier:@"pool2"];
    
    
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 120, 0);
    [self.view addSubview:self.tableView];
    
    
    self.tableView.dk_backgroundColorPicker =  DKColorWithRGB(0xffffff, 0x343434);
    self.tableView.dk_separatorColorPicker = DKColorWithRGB(0xaaaaaa, 0x313131);
    
    [self.tableView release];
    
    self.tableView.showsVerticalScrollIndicator = NO;
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    
    return self.arrModelContent.count + 1 ;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 180;
    }else {
    
    
        return 200;
    }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    
    if (indexPath.row == 0) {
        
        HeadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
       
        
        cell.delegate = self;
        [cell passModel:self.arrModelData];
        
        return cell;
    }
    else {
    
        CellofContent *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
        

        
        
        Modelcontent *model = [self.arrModelContent objectAtIndex:indexPath.row - 1];
        
        
        cell.labelOftitle.text = model.title;
        
        cell.selectionStyle = UITableViewCellAccessoryNone;
        [cell passTwoModel:model];
        
        
        return cell;
    
    }
    
}

- (void)clickImageView:(HeadImageView *)imageView {
    
    PushVc1 *push = [[PushVc1 alloc]init];
    
 
    NSInteger i = imageView.tag-1;
    
   [push passModel:[self.arrModelData objectAtIndex:i]];
      [ self.navigationController pushViewController:push animated:YES];

    
    



}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{

    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:editing];
    
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{



    URLViewController *url = [[URLViewController alloc]init];
    
    Modelcontent *model = [self.arrModelContent objectAtIndex:indexPath.row  -1];
    [url passSecondModel:model];
    
    
    [self.navigationController pushViewController:url animated:YES];
    
    
    


}

-(void)tableView:(UITableView *)tableView willDisplayCell:(CellofContent *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //
//    EveryDayModel *model = self.selectDic[self.dateArray[indexPath.section]][indexPath.row];
    //
    //
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.layer.shadowOffset = CGSizeMake(10, 10);
    cell.alpha = 0;
    
    
    [UIView beginAnimations:@"rotation" context:NULL];
    //旋转时间
    [UIView setAnimationDuration:0.6];
    cell.layer.transform = CATransform3DIdentity;
    cell.alpha = 1;
    cell.layer.shadowOffset = CGSizeMake(0, 0);
    [UIView commitAnimations];
    
//    [cell cellOffset];
//    cell.model = model;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
